﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Collections;
using WebSilverlight.Web;
using WebSilverlight.Helpers;

namespace WebSilverlight.Views
{
    public partial class PaisWorkWith : Page
    {
	#region Properties	
     
	#endregion
	
        public PaisWorkWith()
        {            
            InitializeComponent();
            if(this.dcPaisContext1 == null)                
				this.dcPaisContext1 = new PaisDomainContext();            
        }    
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {            
        }                
        private void AddNew_Click(object sender, RoutedEventArgs e)
        {
			CallEdit(false, true);
        }
        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
             CallEdit(true, false);
        }
        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
             CallEdit(false, false);
        }
        private void CallEdit(bool IsReadOnly, bool IsNew)
        {
			PaisWrapper current = null;
            bool error = false;
            IList list = dataGridPais1.SelectedItems;
            
            if (IsNew)
                 current = new PaisWrapper();
            else
            {
                 if (list.Count != 1)
                 {
                     System.Windows.MessageBox.Show("select one element");
                     error = true;
                 }
                 else
                     current = (PaisWrapper)list[0];
            }
            if (!error)
			{   
				 if (ddsPais1.HasChanges)
				 {                     
					 System.Windows.MessageBox.Show("Changes Discarded");                     
					 this.dataGridPais1.CancelEdit();
					 this.ddsPais1.RejectChanges();
				 }                

				 var editw = new PaisEditWindow(!IsReadOnly);				 
				 editw.DomainContext = this.dcPaisContext1;
				 editw.DDS = this.ddsPais1;
				 editw.Current = current;
				 editw.IsNew = IsNew;
				 if(IsNew)
                     editw.Closed += new EventHandler(editw_Closed);
				 editw.Show();
			 }       
        }

        void editw_Closed(object sender, EventArgs e)
        {   
			//Duplicate Entity Key W.A.
			this.dcPaisContext1 = new PaisDomainContext();            
            this.ddsPais1.Load();            
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
			if(this.dataGridPais1.SelectedItems.Count == 1)
            {
                PaisWrapper current = (PaisWrapper)this.dataGridPais1.SelectedItem;
                this.dcPaisContext1.PaisWrappers.Remove(current);
            }

        }
        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {        				 
			this.dataGridPais1.CommitEdit(DataGridEditingUnit.Row, true);
            this.ddsPais1.SubmitChanges();
            this.GridActionsBottom.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void CancelEditButton_Click(object sender, RoutedEventArgs e)
        {
			if (ddsPais1.HasChanges)
			{			
				 dataGridPais1.CancelEdit();
				 ddsPais1.RejectChanges();
			}
			this.GridActionsBottom.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void dataGridPais1_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            this.GridActionsBottom.Visibility = System.Windows.Visibility.Visible;
        } 
	
		 		
    }
}
